<?php
    class caching
    {
        public static function getCache($sCacheId = '')
        {
            if(isset(core::$aParams[$sCacheId]))
            {
                return core::$aParams[$sCacheId];
            }
            $sFile = "cache/".$sCacheId."Cache";
            if(@file_exists($sFile) ) 
            {
                $sData = file_get_contents($sFile);
                $data = unserialize(($sData));
                core::$aParams[$sCacheId] = $data;
                return $data;
            }
            return false;
        }

        public static function setCache($sCacheId = '', $data)
        {
            core::$aParams[$sCacheId] = $data;
            $sFile = "cache/".$sCacheId."Cache";
            $sData = (serialize($data));

            $wfile = fopen($sFile,"w");
            fwrite($wfile,$sData);
            fclose($wfile);
        }

        public static function hasCache($sCacheId = '')
        {
            if(@file_exists("cache/".$sCacheId."Cache") ) 
            {
                return true;
            }
            return false;
        }

        /**
        *   SOME FUNCTION CREATE CACHE
        */
        public static function createCacheMenu()
        {
            $oDatabase = new database();
            $sQuery = " 
            PREFIX foaf: <http://xmlns.com/foaf/0.1/>
            PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
            PREFIX bus: <".URL::getLink("id/bussiness#").">

            SELECT * 
            WHERE
            {
            ?uri rdf:type bus:category.
            ?uri bus:name ?name .
            ?uri bus:defaultImage ?image.
            OPTIONAL { ?uri bus:subCategory ?pCategory}
            } ";

            $aResults = $oDatabase->loadQuery($sQuery);
            if(!empty($aResults['result']['rows']))
            {
                $aCategories = $aResults['result']['rows'];
                $aMenus = array();
                $aMenus['child'] = array();
                $aMenus['count'] = count($aCategories);
                $aTemps = array();
                foreach($aCategories as $key => $aCategory)
                {
                    $sId = end(explode('/',$aCategory['uri']));
                    $aCategories[$key]['sId'] = $sId;
                }
                while(count($aCategories) > 0)
                {
                    foreach($aCategories as $key => $aCategory)
                    {
                        if(!isset($aCategory['pCategory']))
                        {
                            $aMenus['child'][$aCategory['uri']] = $aCategory;
                            $aMenus['pRoot'][$aCategory['uri']] = $aCategory;
                            $aTemps[$aCategory['uri']] = &$aMenus['child'][$aCategory['uri']];
                            unset($aCategories[$key]);
                        }
                        else
                        {
                            if(isset($aTemps[$aCategory['pCategory']]))
                            {
                                $aTemps[$aCategory['pCategory']]['child'][$aCategory['uri']] = $aCategory; 
                                $aMenus['pRoot'][$aCategory['uri']] = $aMenus['pRoot'][$aCategory['pCategory']];
                                $aTemps[$aCategory['uri']] = &$aTemps[$aCategory['pCategory']]['child'][$aCategory['uri']];
                                unset($aCategories[$key]);
                            }
                        }
                    }
                }

                $aMenus['sub'] = $aTemps;

                caching::setCache('aCategories',$aMenus);
                return $aMenus;
            }
            return array(
                'child' => array(),
                'count' => 0,
                'sub' => array(),
                'pRoot' => array(),
            );
        }

        public static function createCacheHotProducts()
        {
            $oDatabase = new database();
            $uri = URL::getLink('id/hotProduct');
            $sQuery = "
            PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
            PREFIX foaf: <http://xmlns.com/foaf/0.1/>
            PREFIX bus: <".URL::getLink('id/bussiness#').">

            SELECT DISTINCT *  
            WHERE 
            {
            ?uri bus:hasProduct ?product.
            ?product bus:name ?name.
            ?product bus:ownUser ?userUri.
            ?userUri foaf:familyName ?userName.
            ?product bus:hasCategory ?categoryUri.
            ?categoryUri bus:name ?categoryName.
            OPTIONAL{?product bus:hotImage ?image.}
            OPTIONAL{?product bus:defaultImage ?defaultImage.}
            FILTER(?uri = '".$uri."').
            }";
            $aResult = $oDatabase->loadQuery($sQuery);
            caching::setCache('aHotProducts',$aResult['result']['rows']);
            return $aResult['result']['rows'];
        }
    }
?>
